package perform.query.impl;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import perform.exception.QueryException;
import perform.mappings.ClassMappings;
import perform.query.AbstractQuery;
import perform.query.builder.QueryBuilder;
import perform.type.Type;

public class UpdateQuery extends AbstractQuery {
	
	public UpdateQuery(Type type, Connection connection) throws QueryException{
		super();
		try {
			query = QueryBuilder.getUpdateQuery(type.getClass());
			preparedStatement = connection.prepareStatement(query);
			Map<Field, String> allColumns = ClassMappings.getAllColumns(type.getClass());
			List<Field> allFields = new ArrayList<Field>(allColumns.keySet());
			for(int i = 0; i < allFields.size(); i++){
				setValue(i + 1, allFields.get(i).get(type));
			}
			setValue(allFields.size() + 1, ClassMappings.getIDField(type.getClass()).get(type));
		} catch (Exception e) {
			throw new QueryException(e);
		}
	}

}
